package ua.com.stormlabs.jnardi.view;

import ua.com.stormlabs.jnardi.model.Chip;

import javax.swing.*;

import org.apache.log4j.Logger;

/**
 * User: denis
 * Date: Mar 25, 2009
 */
public class BoardForm {

    private static final Logger log = Logger.getLogger(BoardForm.class);

    private JFrame frame;
    private JPanel panel;
    private static final int CENTRAL_BORDER = 2 * ChipLabel.CHIP_WIDTH;
    private static final int SIDE_BORDER = 4 * ChipLabel.CHIP_WIDTH;
    private static final int TOP_BORDER = 1 * ChipLabel.CHIP_HEIGHT;
    private static final int FRAME_WIDTH = 2 * SIDE_BORDER + 12 * ChipLabel.CHIP_WIDTH + CENTRAL_BORDER;
    private static final int FRAME_HEIGHT = 2 * TOP_BORDER + 18 * ChipLabel.CHIP_HEIGHT;

    public BoardForm() {
        frame = new JFrame();
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT + 100);
        log.debug("Frame: " + FRAME_WIDTH + "x" + FRAME_HEIGHT);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel(null);
        //panel.setSize(0, 0);
        //panel.setLocation(0, 0);
        frame.add(panel);
    }

    public void show() {
        frame.setVisible(true);
    }

    public ChipLabel createChipLabel(Chip.Color color, int cell, int level) {
        int x;
        int y;
        if (cell < 12) {
            x = FRAME_WIDTH - SIDE_BORDER - (cell + 1) * ChipLabel.CHIP_WIDTH;
            y = TOP_BORDER + level * (ChipLabel.CHIP_HEIGHT);
        } else {
            x = SIDE_BORDER + (cell - 12) * ChipLabel.CHIP_WIDTH;
            y = FRAME_HEIGHT - TOP_BORDER - (level + 1) * (ChipLabel.CHIP_HEIGHT);
        }
        String icon = "black.gif";
        if (color == Chip.Color.WHITE) {
            icon = "white.gif";
        }
        JLabel label = new JLabel(new ImageIcon(icon));
        label.setText("" + level);
        label.setSize(ChipLabel.CHIP_WIDTH, ChipLabel.CHIP_HEIGHT);
        label.setLocation(x, y);
        log.debug("Label: " + icon + "(" + x + ";" + y + ")");
        //frame.add(label);
        panel.add(label);
        return new ChipLabel(label);
    }
}
